package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: MyWalletActivity
 * @Description: 我的钱包
 * @Author: ljp
 * @CreateDate: 2018/9/19 14:45
 * @Version: 1.0
 */
public class MyWalletActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.view_pager_wallet)
    ViewPager viewPagerWallet;
    @BindView(R.id.linear_my_recharge_record)
    LinearLayout linearMyRechargeRecord;
    @BindView(R.id.linear_my_expense_record)
    LinearLayout linearMyExpenseRecord;
    @BindView(R.id.linear_my_income_record)
    LinearLayout linearMyIncomeRecord;
    @BindView(R.id.linear_my_with_record)
    LinearLayout linearMyWithRecord;
    @BindView(R.id.linear_my_gif_record)
    LinearLayout linearMyGifRecord;

    TextView tvMyGoldSum;
    TextView tvMyRecharge;
    TextView tvMyDiamondSum;
    TextView tvMyWithdraw;
    TextView tvWithdraw;
    private List<View> views;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private String token;
    private String deviceInfo;
    private String TAG = "MyWalletActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_my_wallet;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
        initHttp();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initHttp();
    }

    /**
    * @method
    * @description 余额请求
    * @date: 2018/11/7 14:39
    * @author: ljp
    * @return
    */
    private void initHttp() {
        MeInfoService.goldDiamond(userId,token,deviceInfo).subscribe(goldBean -> {
            //Log.i(TAG, "initHttp: -------"+goldBean.getObject().toString());
            if(goldBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                tvMyGoldSum.setText(goldBean.getObject().getGold()+"");
                tvMyDiamondSum.setText(goldBean.getObject().getDiamonds()+"");
                double div = Utils.div(goldBean.getObject().getDiamonds(), 10, 2);
                tvWithdraw.setText("可提现金额:"+div+"元");
            }else if(goldBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(goldBean.getMsg());
            }
        },throwable -> {
            Log.i(TAG, "initHttp: -----"+throwable.toString());
        });
    }

    /**
    * @method
    * @description viewPager布局实例化
    * @date: 2018/10/8 19:58
    * @author: ljp
    * @return
    */
    private void initView() {
        views = new ArrayList<>();
        View view = View.inflate(context, R.layout.viewpager_item_wallet, null);
        View view2 = View.inflate(context, R.layout.viewpager_item2_wallet, null);
        //充值
        tvMyGoldSum = view.findViewById(R.id.tv_my_gold_sum);
        tvMyRecharge = view.findViewById(R.id.tv_my_recharge);
        //提现
        tvMyDiamondSum = view2.findViewById(R.id.tv_my_diamond_sum);
        tvMyWithdraw = view2.findViewById(R.id.tv_my_withdraw);
        tvWithdraw = view2.findViewById(R.id.tv_withdraw);
        views.add(view);
        views.add(view2);
        viewPagerWallet.setAdapter(new PagerAdapterItem());
        initViews();
    }
    /**
    * @method
    * @description viewpager布局控件实例化
    * @date: 2018/10/8 19:58
    * @author: ljp
    * @return
    */
    private void initViews() {
        tvMyRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(activity,WebPageActivity.class));
                Intent intent = new Intent(activity, GoldDepositsActivity.class);
                intent.putExtra("diamondSum",Integer.parseInt(tvMyGoldSum.getText().toString()));
                startActivity(intent);
                finish();
               /* Uri uri = Uri.parse(HTTPAPI.URL_RECHARGE+"id="+mySeleInfo.getNumber());
                Log.i(TAG, "onClick: --------"+uri.toString());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);*/
            }
        });
        tvMyWithdraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initJude();
               /* Intent intent = new Intent(activity, WithdrawActivity.class);
                intent.putExtra("diamondSum",Integer.parseInt(tvMyDiamondSum.getText().toString()));
                startActivity(intent);*/
            }
        });
    }
    /**
    * @method
    * @description 判断是否绑定支付宝账号
    * @date: 2018/11/29 11:12
    * @author: ljp
    * @return
    */
    private void initJude() {
        MeInfoService.queryAccount(mySeleInfo.getId(), SPUtils.getEditoString(SPUtils.SP_TOKEN), PhoneUtils.getDeviceInfo(context)).subscribe(mySelfInfoBean -> {
            //Log.i(TAG, "initHttp: -----"+mySelfInfoBean.getObject().toString());
            if (mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                //accountId = mySelfInfoBean.getObject().getAccountId();
                if (!TextUtils.isEmpty(mySelfInfoBean.getObject().getAccount())) {
                    Intent intent = new Intent(activity, WithdrawActivity.class);
                    intent.putExtra("account",mySelfInfoBean.getObject().getName());
                    Log.i(TAG, "initJude: --getName--"+mySelfInfoBean.getObject().getName());
                    intent.putExtra("diamondSum",Integer.parseInt(tvMyDiamondSum.getText().toString()));
                    startActivity(intent);
                }else {
                    toast_MakeText("请先绑定支付宝账号");
                }
            }else if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(mySelfInfoBean.getMsg());
            }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
            //Log.i(TAG, "initHttp: -----" + throwable.toString());
        });
    }
    @OnClick({R.id.linear_back, R.id.linear_my_recharge_record, R.id.linear_my_expense_record, R.id.linear_my_income_record, R.id.linear_my_with_record, R.id.linear_my_gif_record})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.linear_back:
                finish();
                break;
            case R.id.linear_my_recharge_record://充值记录
                startActivity(new Intent(activity,RechargeRecordActivity.class));
                break;
            case R.id.linear_my_expense_record://消费记录
                startActivity(new Intent(activity,ExpenseRecordActivity.class));
                break;
            case R.id.linear_my_income_record://金币明细
                startActivity(new Intent(activity,IncomeRecordActivity.class));
                break;
            case R.id.linear_my_with_record://提现记录
                startActivity(new Intent(activity,WithdrawRecordActivity.class));
                break;
            case R.id.linear_my_gif_record://钻石明细
                startActivity(new Intent(activity,MyGifRecordActivity.class));
                break;
        }
    }


    class PagerAdapterItem extends PagerAdapter {
        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        public float getPageWidth(int position) {
            /*if (position == 0 || position == 2) {
                return 0.85f;
            }*/
            return 0.85f;
        }
    }
}
